from skimage import io,filters,img_as_ubyte, feature
import matplotlib.pyplot as plt
import cv2
img = io.imread('camera.png', as_gray=True) # 读取灰度图
plt.subplot(2, 3, 1), plt.imshow(img,cmap='gray'),plt.title('original'), plt.axis('off')
# sobel 边缘检测
edges_sobel = filters.sobel(img)
plt.subplot(2, 3, 2), plt.imshow(edges_sobel, cmap='gray'), plt.title('sobel'), plt.axis('off') # prewitt 边缘检测
edges_prewitt = filters.prewitt(img)
plt.subplot(2, 3, 3), plt.imshow(edges_prewitt, cmap='gray'), plt.title('prewitt'), plt.axis('off')
# log 边缘检测 先通过高斯滤波降噪
gaussian = cv2.GaussianBlur(img, (3, 3), 0)
# 再通过拉普拉斯算子做边缘检测
dst = cv2.Laplacian(gaussian, cv2.CV_16S, ksize=3)
LOG = cv2.convertScaleAbs(dst)
plt.subplot(2, 3, 4), plt.imshow(LOG, cmap='gray'), plt.title('log'), plt.axis('off')
# canny 边缘检测
edges_canny = feature.canny(img, sigma=1.0)
plt.subplot(2, 3, 5), plt.imshow(edges_canny, cmap='gray'), plt.title('canny'), plt.axis('off')
plt.show()

